//
//  JBChineseAndPinYin.m
//  JBPinYin
//
//  Created by YongbinZhang on 7/10/13.
//  Copyright (c) 2013 JustBen. All rights reserved.
//

#import "JBChineseAndPinYin.h"

@interface JBChineseAndPinYin ()

/**************************************************
 *@Description:获取字典数组中，关键字char的值为value的字典
 *@Params:
 *  value:字典中的关键字char对应的值
 *  array:字典数组
 *@Return:字典
 **************************************************/
+ (NSDictionary *)dictionaryWithCharKeyValue:(NSString *)value InArray:(NSArray *)array;

@end

@implementation JBChineseAndPinYin

#pragma mark -
#pragma mark - Class Extensions
/**************************************************
 *@Description:获取字典数组中，关键字char的值为value的字典
 *@Params:
 *  value:字典中的关键字char对应的值
 *  array:字典数组
 *@Return:字典
 **************************************************/
+ (NSDictionary *)dictionaryWithCharKeyValue:(NSString *)value InArray:(NSArray *)array
{
    if (value && value.length > 0 && array && array.count > 0) {
        for (NSDictionary *dic in array) {
            if ([[dic objectForKey:SortedChineseAndPinYinKeyChar] isEqualToString:value]) {
                return dic;
            }
        }
    }
    
    return nil;
}


#pragma mark -
#pragma mark - Class Methods
/***************************************************
 *@Description:将中文字符串数组转换为拼音与中文一一对应的字典数组
 *@Params:
 *  ChineseDataSource:中文字符串数组
 *@Return:拼音与中文的字典数组
 ***************************************************/
+ (NSArray *)ChineseAndPinYinArrayFromChineseDataSource:(NSArray *)ChineseDataSource
{
    if (ChineseDataSource) {
        NSMutableArray *ChineseAndPinYinArray = [[NSMutableArray alloc] init];
        for (NSString *ChineseStr in ChineseDataSource) {
            int pinYinLength = ChineseStr.length;
            char pinYin[pinYinLength + 1];
            int pinYinValueLen = 0;
            for (int i = 0; i < ChineseStr.length; i++) {
                char var = pinyinFirstLetter([ChineseStr characterAtIndex:i]);
                if ((var >= 'a' && var <= 'z') || (var >= 'A' && var <= 'Z') || (var >= '0' && var <= '9')) {
                    pinYin[pinYinValueLen++] = var;
                }
            }
            pinYin[pinYinValueLen] = '\0';
            
            NSDictionary *ChineseAndPinYin = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:ChineseStr, [NSString stringWithUTF8String:pinYin], nil] forKeys:[NSArray arrayWithObjects:ChineseAndPinYinKeyChinese, ChineseAndPinYinKeyPinYin, nil]];
            [ChineseAndPinYinArray addObject:ChineseAndPinYin];
        }
        
        return ChineseAndPinYinArray;
    }
    
    return nil;
}


/***************************************************
 *@Description:将中文字符串数组转换为拼音与中文一一对应的
 字典数组并按照拼音排序
 *@Params:
 *  ChineseDataSource:中文字符串数组
 *@Return:拼音与中文的字典数组并按照拼音排序
 ***************************************************/
+ (NSArray *)SortedChineseAndPinYinArrayFromChineseDataSource:(NSArray *)ChineseDataSource
{
    if (ChineseDataSource) {
        NSMutableArray *ChineseAndPinYinArray = [[NSMutableArray alloc] init];

        for (NSString *ChineseStr in ChineseDataSource) {
            int pinYinLength = ChineseStr.length;
            char pinYin[pinYinLength + 1];
            int pinYinValueLen = 0;
            for (int i = 0; i < ChineseStr.length; i++) {
                char var = pinyinFirstLetter([ChineseStr characterAtIndex:i]);
                if ((var >= 'a' && var <= 'z') || (var >= 'A' && var <= 'Z') || (var >= '0' && var <= '9')) {
                    pinYin[pinYinValueLen++] = var;
                }
            }
            pinYin[pinYinValueLen] = '\0';
            
            NSDictionary *ChineseAndPinYin = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:ChineseStr, [NSString stringWithUTF8String:pinYin], nil] forKeys:[NSArray arrayWithObjects:ChineseAndPinYinKeyChinese, ChineseAndPinYinKeyPinYin, nil]];

            NSMutableDictionary *ChineseAndPinYinDic;
            NSString *key;
            NSMutableArray *value = [[NSMutableArray alloc] init];
            if ((strlen(pinYin) > 0) && ((pinYin[0] >= 'a' && pinYin[0] <= 'z') || (pinYin[0] >= 'A' && pinYin[0] <= 'Z'))) {
                key = [[NSString stringWithFormat:@"%c", pinYin[0]] uppercaseString];
            } else {
                key = @"#";
            }
            
            ChineseAndPinYinDic = (NSMutableDictionary *)[self dictionaryWithCharKeyValue:key InArray:ChineseAndPinYinArray];
            [value addObject:ChineseAndPinYin];
            if (ChineseAndPinYinDic) {
                [value addObjectsFromArray:[ChineseAndPinYinDic objectForKey:SortedChineseAndPinYinKeyValue]];
                [ChineseAndPinYinDic setValue:value forKey:SortedChineseAndPinYinKeyValue];
            } else {
                ChineseAndPinYinDic = [[NSMutableDictionary alloc] initWithObjects:[NSArray arrayWithObjects:key, value, nil] forKeys:[NSArray arrayWithObjects:SortedChineseAndPinYinKeyChar, SortedChineseAndPinYinKeyValue, nil]];
                [ChineseAndPinYinArray addObject:ChineseAndPinYinDic];
            }
        }
        
//        NSMutableArray *arr = [[NSMutableArray alloc] init];
        for (NSMutableDictionary *ChineseAndPinYinDic in ChineseAndPinYinArray) {
//            NSString *charStr = [ChineseAndPinYinDic objectForKey:SortedChineseAndPinYinKeyChar];
            NSArray *value = [ChineseAndPinYinDic objectForKey:SortedChineseAndPinYinKeyValue];
            NSArray *sortedValue = [value sortedArrayUsingSelector:@selector(compareChineseAndPinYin:)];
            [ChineseAndPinYinDic setValue:sortedValue forKey:SortedChineseAndPinYinKeyValue];
//            NSDictionary *dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:charStr, sortedValue, nil] forKeys:[NSArray arrayWithObjects:SortedChineseAndPinYinKeyChar, SortedChineseAndPinYinKeyValue, nil]];
//            [arr addObject:dic];
        }
        
        return [ChineseAndPinYinArray sortedArrayUsingSelector:@selector(compareChineseAndPinYinDic:)];
    }
    
    return nil;
}

@end



@implementation NSDictionary (PinYin)

/*
 {
 pinyin:1aa
 chinese:1啊啊
 }
 根据pingyin字段排序
 */
- (NSComparisonResult)compareChineseAndPinYin:(NSDictionary *)otherDictionary
{
    NSString *pinyin = [self objectForKey:ChineseAndPinYinKeyPinYin];
    NSString *otherPinYin = [otherDictionary objectForKey:ChineseAndPinYinKeyPinYin];
    
    return [pinyin caseInsensitiveCompare:otherPinYin];
}

/*
 {
 char:A
 value:
 [
 {
 pinyin:aa
 chinese:啊啊
 },
 {
 pinyin:Aa
 chinese:A阿
 }
 ]
 }
 根据char字段排序
 */
- (NSComparisonResult)compareChineseAndPinYinDic:(NSDictionary *)otherDicationary
{
    NSString *charStr = [self objectForKey:SortedChineseAndPinYinKeyChar];
    NSString *otherCharStr = [otherDicationary objectForKey:SortedChineseAndPinYinKeyChar];
    
    if ([charStr isEqualToString:otherCharStr]) {
        return NSOrderedSame;
    }
    
    if ([charStr isEqualToString:@"#"]) {
        return NSOrderedDescending;
    }
    
    if ([otherCharStr isEqualToString:@"#"]) {
        return NSOrderedAscending;
    }
    
    return [charStr caseInsensitiveCompare:otherCharStr];
}

@end